//
//  FWAnchorViewController.swift
//  DBS
//
//  Created by 王方伟 on 2017/4/20.
//  Copyright © 2017年 wfw. All rights reserved.
//

import UIKit
import DCURLRouter

private let kAnchorCellID = "FWHomeViewCell"

class FWAnchorViewController: UIViewController {
    var homeType : FWHomeTypeModel!
    lazy var dataSource : [FWAnchorModel] = [FWAnchorModel]()
    
    fileprivate lazy var collectionView : UICollectionView = {
        let layout = FWWaterflowLayout()
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 200, 10)
        layout.minimumInteritemSpacing = 10
        layout.minimumLineSpacing = 10
        layout.columnCount = 2
        layout.delegate = self
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(UINib(nibName: "\(FWHomeViewCell.self)", bundle: nil), forCellWithReuseIdentifier: kAnchorCellID)
        collectionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        collectionView.backgroundColor = UIColor.white
        
        return collectionView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(collectionView)
        FWHomeService.loadHomeData(type: homeType, index: 0, success: { (dataArray) in
            self.dataSource = dataArray;
            self.collectionView.reloadData()
        }) { (error) in}
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
}


extension FWAnchorViewController : UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kAnchorCellID, for: indexPath) as! FWHomeViewCell
        cell.modle = dataSource[indexPath.item]
        return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let roomVC = FWRoomViewController()
        DCURLRouter.push(roomVC, animated: true)
    }
}

extension FWAnchorViewController : WaterflowLayoutDelegate {
    func waterflowLayout(_ waterflowLayout: FWWaterflowLayout, index: Int, itemWidth: CGFloat) -> CGFloat {
        return index % 2 == 0 ? view.bounds.width * 2 / 3 : view.bounds.width * 0.5
    }
    
}
